package cn.kilo.trigger.http.pay;

import cn.kilo.domain.service.pay.IWxChatPayService;
import cn.kilo.trigger.api.IWeChatPayService;
import cn.kilo.trigger.api.dto.weChatPay.PayDTO;
import cn.kilo.types.enums.ResponseCode;
import cn.kilo.types.model.Response;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author kilok
 */
@RestController
@Slf4j
@CrossOrigin("*")
@RequestMapping("wx/")
public class weChatPayController implements IWeChatPayService {

    @Resource
    @Qualifier("WXPayService")
    private IWxChatPayService wxChatPayService;

    @GetMapping("/getPayInfo")
    public Response<?> getPayInfo(){
        try{
            wxChatPayService.getPay();
            return null;
        }catch (Exception e ){
            e.printStackTrace();
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data("获取信息失败")
                    .build();
        }
    }


    @PostMapping("callBack")
    @Override
    public void callBack(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
       try {
           wxChatPayService.callBack(httpServletRequest,httpServletResponse);
       }catch (Exception e){
            e.printStackTrace();
       }
    }

    @PostMapping("/H5Pay")
    @Override
    public Response<?> H5Pay(@RequestBody PayDTO payDTO) {
        try {
            String url = wxChatPayService.H5Pay(payDTO.getPrice(), payDTO.getOrderId(), payDTO.getIp());
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(url)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("支付失败")
                    .build();
        }
    }

    @PostMapping("/getPayInfo")
    @Override
    public Response<?> getPayInfo(@RequestParam("orderId") Long orderId) {
        try{

            String orderNo=String.valueOf(orderId);
            HashMap<String, Transaction> payInfo = wxChatPayService.getPayInfo(orderNo);
            return Response.<HashMap<String, Transaction>>builder()
                    .code(ResponseCode.SUCCESS.getInfo())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(payInfo)
                    .build();
        }catch (Exception e){

            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data("信息查询失败")
                    .build();
        }
    }
    @PostMapping("/closeOrder")
    @Override
    public Response<?> closeOrder(@RequestBody PayDTO payDTO) {
        log.info("取消订单:{}",payDTO.getOrderId());

        wxChatPayService.closeOrder(payDTO.getOrderId());
        return Response.<String>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data("关闭订单号："+payDTO.getOrderId())
                .build();
    }

}
